package server

import (
	"company_website/src/global"
	"company_website/src/model"
	"errors"
	"github.com/goccy/go-json"
)

type adminUser struct{}

func (a *adminUser) GetUserToRedis(token string) (*model.AdminUser, error) {
	userJSON, err := global.GVL_REDIS.Conn.Get(global.GVL_REDIS.Ctx, token).Result()
	if err != nil {
		return nil, err
	}
	// 解析用户信息 JSON
	user := &model.AdminUser{}
	if err := json.Unmarshal([]byte(userJSON), user); err != nil {
		// 如果解析出错，返回错误信息
		return nil, err
	}
	return user, nil
}

// 在数据库中验证机器人logintoken
func (a *adminUser) VerifyLoginToken(LoginToken string) (*model.AdminUser, error) {
	resInfo := &model.AdminUser{}
	db := global.GVL_DB.Model(&model.AdminUser{})
	var count int64
	count = 0
	db.Where("login_token = ?", LoginToken).Count(&count).Find(&resInfo)
	if count == 1 {
		return resInfo, nil
	} else {
		return resInfo, errors.New("token not found")
	}
}
